package zust.se.ssm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import zust.se.ssm.dto.UserDto;
import zust.se.ssm.model.Contacts;
import zust.se.ssm.service.ContactsServiceI;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 
 * @author czg
 * @since 2022/12/14 14:53
 */
@Controller
@Api(value = "联系人管理")
@RequestMapping("contacts")
public class ContactsController {
    //只能依赖service的接口
    @Autowired
    ContactsServiceI contactsServiceI;
    /**
     * 联系人管理首页
     * @author czg
     * @since 2022/12/21 15:03  
    * @return java.lang.String
    */
    @GetMapping("index")
    public ModelAndView index(HttpSession session){
        ModelAndView mav=new ModelAndView();
        UserDto curUser=(UserDto)session.getAttribute("user");
        if(curUser==null){
            mav.setViewName("login");
            return mav;
        }
        mav.setViewName("showContacts");
//        List<Contacts> list=contactsServiceI.getAll();
        List<Contacts> list=contactsServiceI.getUserContacts(curUser.getId());
        mav.addObject("contacts",list);
        return mav;
    }

    /**
     * 查询联系人
     * @author czg
     * @since 2022/12/21 15:06 
     * @param name:
     * @param model:
     * @return java.lang.String
    */
    @GetMapping("search")
    public String searchContacts(HttpSession session,String name, Model model){
        if(StringUtils.isEmpty(name))
            name="";
        UserDto curUser=(UserDto)session.getAttribute("user");
        if(curUser==null){
            return "login";
        }
//        List<Contacts> list=contactsServiceI.searchByName(name);
        List<Contacts> list=contactsServiceI.searchByName(name,curUser.getId());
        model.addAttribute("contacts",list);
        return "showContacts";
    }

    /**
     * 新建联系人页面
     * @author czg
     * @since 2022/12/21 15:09  
    * @return java.lang.String
    */
    @GetMapping("add")
    public String addContacts(){
        return "newContact";
    }
    /**
     * 新建联系人表单数据 处理
     * @author czg
     * @since 2022/12/21 15:09 
     * @param contacts:
     * @return org.springframework.web.servlet.ModelAndView
    */
    @PostMapping("add")
    public String doAddContacts(HttpSession session,Contacts contacts){
        UserDto curUser=(UserDto)session.getAttribute("user");
        // 用户未登录转向登录页面
        if(curUser==null){
            return "login";
        }
        contacts.setUserId(curUser.getId());
        contactsServiceI.addContacts(contacts);
        return "redirect:/contacts/index";
    }
    /**
     * 编辑id对应的用户信息的页面
     * @author czg
     * @since 2022/12/21 15:12 
     * @param id:  路径参数
     * @return java.lang.String
    */
    @GetMapping("/edit/{id}")
    public String editContacts(HttpSession session,@PathVariable("id") int id,Model model){
        UserDto curUser=(UserDto)session.getAttribute("user");
        // 用户未登录转向登录页面
        if(curUser==null){
            return "login";
        }
        Contacts contacts=contactsServiceI.getById(id);
        // 无对应联系人信息或改联系人的用户不是当前用户，转向当前用户的首页
        if(contacts==null||contacts.getUserId()!=curUser.getId())
            return "redirect:/contacts/index";
        model.addAttribute("contacts",contacts);
        return "editContacts";

    }

    /**
     * 完成用户信息编辑
     * @author czg
     * @since 2022/12/21 15:15
     * @param editContacts:
     * @return java.lang.String
    */
    @PostMapping("/edit")
    public String doEditContacts(HttpSession session,Contacts editContacts){
        UserDto curUser=(UserDto)session.getAttribute("user");
        // 用户未登录转向登录页面
        if(curUser==null){
            return "login";
        }
        Contacts contacts=contactsServiceI.getById(editContacts.getId());
        // 无对应联系人信息或改联系人的用户不是当前用户，转向当前用户的首页
        if(contacts==null||contacts.getUserId()!=curUser.getId())
            return "redirect:/contacts/index";
        BeanUtils.copyProperties(editContacts,contacts,new String[]{"id","userId"});
        contactsServiceI.updateContacts(contacts);
        return "redirect:/contacts/index";
    }
    @GetMapping("del")
    public String delContacts(HttpSession session,int id){
        UserDto curUser=(UserDto)session.getAttribute("user");
        // 用户未登录转向登录页面
        if(curUser==null){
            return "login";
        }
        Contacts contacts=contactsServiceI.getById(id);
        // 无对应联系人信息或改联系人的用户不是当前用户，转向当前用户的首页
        if(contacts==null||contacts.getUserId()!=curUser.getId())
            return "redirect:/contacts/index";
        contactsServiceI.delContacts(id);
        return "redirect:/contacts/index";
    }

}
